#!/usr/bin/env bash

# Filename     : ubuntu_change_aliyun_source.sh
# Last modified: 2024-08-23 12:12
# Version      : 1.1
# Author       : jack.zang
# Email        : jack.zang@aishangwei.net
# Description  : 将Ubuntu软件源更换为阿里云镜像源
# Usage        : source <(curl -sL https://gitee.com/jack_zang/public-scripts/raw/master/shell/repo/ubuntu_change_aliyun_source.sh)
# ******************************************************

# 设置日志颜色
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
NC="\033[0m" # No Color

# 日志函数
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查是否为root用户运行
check_root() {
    if [ "$(id -u)" -ne 0 ]; then
        log_error "此脚本需要root权限运行"
        log_info "请使用 sudo 运行此脚本"
        exit 1
    fi
}

# 备份原始源文件
backup_sources() {
    local source_file="$1"
    local backup_file="${source_file}.bak.$(date +%Y%m%d%H%M%S)"
    
    if [ -f "$source_file" ]; then
        log_info "备份 $source_file 到 $backup_file"
        cp "$source_file" "$backup_file" || {
            log_error "备份 $source_file 失败"
            return 1
        }
    else
        log_warn "$source_file 不存在，跳过备份"
    fi
    
    return 0
}

# Ubuntu 24.04 阿里云源配置
ubuntu24_aliyun_sources_list() {
    log_info "配置 Ubuntu 24.04 阿里云镜像源"
    
    if [ -f "/etc/apt/sources.list.d/ubuntu.sources" ]; then
        backup_sources "/etc/apt/sources.list.d/ubuntu.sources" || return 1
        
        log_info "将软件源替换为阿里云镜像"
        sed -i 's/cn.archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list.d/ubuntu.sources
        sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list.d/ubuntu.sources
        
        log_info "Ubuntu 24.04 阿里云镜像源配置完成"
    else
        log_warn "未找到 /etc/apt/sources.list.d/ubuntu.sources 文件"
        return 1
    fi
    
    return 0
}

# Ubuntu 22.04 阿里云源配置
ubuntu22_aliyun_sources_list() {
    log_info "配置 Ubuntu 22.04 阿里云镜像源"
    
    if [ -f "/etc/apt/sources.list" ]; then
        backup_sources "/etc/apt/sources.list" || return 1
        
        log_info "下载阿里云镜像源配置文件"
        curl -o "/etc/apt/sources.list" "https://gitee.com/jack_zang/public-scripts/raw/master/shell/repo/ubuntu22_aliyun_sources.list" || {
            log_error "下载阿里云镜像源配置文件失败"
            return 1
        }
        
        log_info "Ubuntu 22.04 阿里云镜像源配置完成"
    else
        log_warn "未找到 /etc/apt/sources.list 文件"
        return 1
    fi
    
    return 0
}

# 更新软件包列表
update_apt() {
    log_info "更新软件包列表"
    apt-get update || {
        log_error "更新软件包列表失败"
        return 1
    }
    log_info "软件包列表更新成功"
    return 0
}

# 主函数
main() {
    log_info "开始配置阿里云镜像源"
    
    # 检查是否为root用户
    check_root
    
    # 检查是否为Ubuntu系统
    if [ ! -f /etc/lsb-release ]; then
        log_error "此脚本仅适用于Ubuntu系统"
        exit 1
    fi
    
    # 获取Ubuntu版本
    local ubuntu_version=$(grep -oP 'DISTRIB_RELEASE=\K[\d.]+' /etc/lsb-release)
    log_info "检测到Ubuntu版本: $ubuntu_version"
    
    # 根据版本配置对应的源
    case "$ubuntu_version" in
        24.04)
            ubuntu24_aliyun_sources_list || exit 1
            ;;
        22.04)
            ubuntu22_aliyun_sources_list || exit 1
            ;;
        *)
            log_warn "不支持的Ubuntu版本: $ubuntu_version"
            log_info "目前仅支持Ubuntu 22.04和24.04"
            exit 1
            ;;
    esac
    
    # 更新软件包列表
    update_apt || exit 1
    
    log_info "阿里云镜像源配置完成"
}

# 执行主函数
main